﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.Odbc;
using System.Collections;
using System.Collections.Specialized;

namespace BugTracker
{
    class DBWDataSet : IDisposable
    {
        private bool disposed = false;
        private OdbcCommand DbCommand = null;
        private OdbcDataReader DbReader = null;
        private StringCollection ColumnCollection = new StringCollection();

        public bool Read()
        {
            return DbReader.Read();
        }
        public bool NextResult()
        {
            bool ret = DbReader.NextResult();
            if (ret)
            {
                ColumnCollection.Clear();
                int fCount = DbReader.FieldCount;
                for (int i = 0; i < fCount; i++)
                    ColumnCollection.Add(DbReader.GetName(i));
            };
            return ret;
        }
        public bool FieldExists(String fieldName)
        {
            return (ColumnCollection.IndexOf(fieldName) >= 0);
        }
        public string GetString(String fieldName)
        {
            int i = ColumnCollection.IndexOf(fieldName);
            return DbReader.GetString(i);
        }
        public int GetInt32(String fieldName)
        {
            int i = ColumnCollection.IndexOf(fieldName);
            return DbReader.GetInt32(i);
        }
        public bool GetBoolean(String fieldName)
        {
            int i = ColumnCollection.IndexOf(fieldName);
            return DbReader.GetBoolean(i);
        }
        public String GetAnythingAsString(String fieldName)
        {
            String ret = "";
            int i = ColumnCollection.IndexOf(fieldName);
            String type = DbReader.GetDataTypeName(i);
            type = type.ToLower();
            if (type.Equals("varchar"))
                ret = DbReader.GetString(i);
            else if (type.Equals("bigint"))
                ret = DbReader.GetInt64(i).ToString();
            else if (type.Equals("datetime"))
                ret = DbReader.GetDateTime(i).ToString();
            // TODO Встроить и другие типы данных

            return ret;
        }

        public bool ErrorSet()
        {
            if (DbReader == null)
                return false;

            if (FieldExists(constants.FieldErrorCode) && FieldExists(constants.FieldErrorMessage))
                if (GetInt32(constants.FieldErrorCode) != 0)
                    return true;
            return false;
        }
        public int FieldCount
        {
            get
            {
                return ColumnCollection.Count;
            }
        }
        public String GetFieldName(int i)
        {
            return ColumnCollection[i];
        }

        public DBWDataSet(OdbcConnection DBCon, String SQLCommand)
        {
            DbCommand = DBCon.CreateCommand();
            DbCommand.CommandText = SQLCommand;
            DbReader = DbCommand.ExecuteReader();
            int fCount = DbReader.FieldCount;
            for (int i = 0; i < fCount; i++)
                ColumnCollection.Add(DbReader.GetName(i));
        }
        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }
        private void Dispose(bool disposing)
        {
            if (!this.disposed)
            {
                // Clear resources
                if (DbReader != null)
                    DbReader.Close();
                ColumnCollection.Clear();

                if (disposing)
                {
                    // Dispose managed resources.
                    if (DbCommand != null)
                        DbCommand.Dispose();
                    if (DbReader != null)
                        DbReader.Dispose();
                };
    
                // Note disposing has been done.
                disposed = true;
            };
        }
        ~DBWDataSet()
        {
            Dispose(false);
        }
    }
}
